package name.wilbur.course.db.bigone.control;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import name.wilbur.course.db.bigone.dao.DAOFactory;
import name.wilbur.course.db.bigone.dao.impl.CityDAO;
import name.wilbur.course.db.bigone.dao.impl.ICityDAO;
import name.wilbur.course.db.bigone.model.City;
import name.wilbur.course.db.bigone.model.User;
import name.wilbur.course.db.bigone.service.IPrepareService;
import name.wilbur.course.db.bigone.service.IUserService;
import name.wilbur.course.db.bigone.service.impl.PrepareService;
import name.wilbur.course.db.bigone.service.impl.UserService;
import name.wilbur.course.db.bigone.ui.custom.ListModel;

public class AdminMgr extends BaseMgr {
	private List<User> removedUserList;
	private ListModel<User> userListModel;
	private ListModel<City> cityListModel;
	private IUserService userServ;
	private IPrepareService prepareServ;
	
	public AdminMgr(Map<String, Object> session) {
		DAOFactory.cleanup();
		DAOFactory.initialize(DAOFactory.DBA_LEVEL.ADMIN);
		
		setSession(session);
		
		userServ = new UserService();
		prepareServ = new PrepareService();
		userListModel = new ListModel<User>(Arrays.asList(userServ.getAllUsersExcept(getMe().getPk())));
		cityListModel = new ListModel<City>(Arrays.asList(prepareServ.getAllCitys()));
		
		removedUserList = new ArrayList<User>();
	}
	
	public ListModel<City> importCityListFile(String filename) {
		List<City> cityList = new ArrayList<City>();
		Scanner sc = null;
		try {
			sc = new Scanner(new File(filename), "UTF-8");
			sc.useDelimiter(System.getProperty("line.separator"));
			String []toks;
			String line;
			int cityCode;
			String cityName;
			City city;
			while (sc.hasNextLine()) {
				line = sc.nextLine();
				toks = line.split(" ");
				if (toks.length == 2) {
					cityCode = Integer.parseInt(toks[0]);
					cityName = toks[1];
					city = new City(cityName, cityCode);
					cityList.add(city);
				}
			}
		} catch (FileNotFoundException e) {
			System.err.println("File " + filename + "does not exist.");
			e.printStackTrace();
		} finally {
			if (sc != null) sc.close();
		}
		return(cityListModel = new ListModel<City>(cityList));
	}
	
	public void removeUser(ListModel<User> users, String userName) {
		User tmp = userServ.getUser(userName);
		users.remove(tmp);
		removedUserList.add(tmp);
	}
	
	public void applyChange() {
		if (!removedUserList.isEmpty()) {
			userServ.removeUsers(removedUserList.toArray(new User[1]));
			removedUserList.clear();
		}
		ICityDAO cityDAO = (ICityDAO) DAOFactory.createDAO(CityDAO.class);
		cityDAO.clearDB();
		cityDAO.create(cityListModel.getContent().toArray(new City[1]));
	}
	
	public ListModel<City> clearCityList() {
		return(cityListModel = new ListModel<City>());
	}
	
	public ListModel<City> getCityList() {
		return cityListModel;
	}
	
	public ListModel<User> getUserList() {
		return userListModel;
	}

	public boolean deleteFriend(User user) {
		return userServ.removeUser(user.getPk());
	}
}
